package house.dao.impl;

import java.sql.Date;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;



import house.basedao.HibernateSessionFactory;
import house.dao.HouseDao;
import house.entity.District;
import house.entity.House;
import house.entity.HousePicture;
import house.entity.HouseType;
import house.entity.Street;
import house.entity.UpLoadFile;

public class HouseDaoImpl implements HouseDao {

	@Override
	public boolean save(House house) {
		Transaction tx=null;
		Session session=HibernateSessionFactory.getSession();
			try {
				 tx=session.beginTransaction();
				 session.save(house);
				 tx.commit();
			} catch (Exception e) {
				e.printStackTrace();
				if (tx != null) {
					tx.rollback();
				}
			}finally{
				HibernateSessionFactory.closeSession();
			}
		return true;
	}

	@Override
	public void delete(House house) {
		Transaction tx=null;
		Session session=HibernateSessionFactory.getSession();
		try {
			tx=session.beginTransaction();
			session.delete(house);
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			if(tx!=null){
				tx.rollback();
			}
		}finally{
			HibernateSessionFactory.closeSession();
		}

	}

	@Override
	public boolean update(House house) {
		Transaction tx=null;
		Session session=null;
		session=HibernateSessionFactory.getSession();
		try {
			tx=session.beginTransaction();
			session.update(house);
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			if (tx!=null) {
				tx.rollback();
			}
		}finally{
			HibernateSessionFactory.closeSession();
		}
		return true;
	}

	@Override
	public House findById(Integer id) {
		House house=null;
		Session session=HibernateSessionFactory.getSession();
		try {
			house=(House) session.get(House.class, id);
		} catch (HibernateException e) {
			e.printStackTrace();
		}finally{
			HibernateSessionFactory.closeSession();
		}
		return house;
	}

	public HousePicture getPictureById(Integer id) {
		HousePicture housePic=null;
		Session session= HibernateSessionFactory.getSession();
		try {
			housePic=(HousePicture) session.get(HousePicture.class, id);
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			HibernateSessionFactory.closeSession();
		}
		return housePic;
	}

	@Override
	public Object[] findAll(Map<String, Object> params) {
		Session session=HibernateSessionFactory.getSession();
		House[] houses=null;
		//houses=session.get(houses, arg1, arg2)
		return null;
	}

	@Override
	public List<HouseType> findTypeList() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<District> findDistrictList() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Street> findStreetListByDisId(int disId) {
		// TODO Auto-generated method stub
		return null;
	}
	 public static void main(String[] args) {
		HouseDaoImpl houseDaoImpl=new HouseDaoImpl();
		House house=new House();
//		house.setDescriprion("一间好房");
//		house.setPrice(10000d);
//		house.setContact("1488445");
//		house.setFloorage(100.5f);
//		house.setPubdate(Date.valueOf("2016-4-9"));
//		house.setTitle("南北朝向");
//		houseDaoImpl.save(house);
		 house=houseDaoImpl.findById(1);
		 System.out.println(house.getTitle());
	}
}
